package com.xr.core.system.core.aop;

import com.xr.core.system.common.annotation.NoRepeatSubmit;
import com.xr.core.system.common.base.Result;
import com.xr.core.system.common.enums.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Slf4j
@Aspect
@Component
public class RepeatSubmitAspect {

    @Autowired
    private RedissonClient redissonClient;

    /**
     * redis 保存前缀
     */
    private static final String PREFIX = "repeatSubmit_";

    @Pointcut("@annotation(noRepeatSubmit)")
    public void pointCut(NoRepeatSubmit noRepeatSubmit) {
    }

    @Around(value = "pointCut(noRepeatSubmit)", argNames = "pjp,noRepeatSubmit")
    public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) throws Throwable {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 从获取RequestAttributes中获取HttpServletRequest的信息
        Assert.notNull(requestAttributes, "requestAttributes can not be null");
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        Assert.notNull(request, "request can not be null");
        // 此处可以用token或者JSessionId
        String token = request.getHeader("token");
        String path = request.getServletPath();
        String key = PREFIX + token + "_" + path;
        RLock lock = redissonClient.getLock(key);
        boolean isSuccess = lock.tryLock(noRepeatSubmit.lockTime(), TimeUnit.SECONDS);
        log.info("tryLock key = [{}],", key);
        if (isSuccess) {
            log.info("tryLock success, key = [{}]", key);
            // 获取锁成功
            Object result;
            try {
                // 执行进程
                result = pjp.proceed();
            } finally {
                // 解锁
                lock.unlock();
                log.info("releaseLock success, key = [{}] ", key);
            }
            return result;
        } else {
            // 获取锁失败，认为是重复提交的请求
            log.info("tryLock fail, key = [{}]", key);
            return Result.res(ResultEnum.REPEAT_SUBMIT);
        }
    }

}
